package com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.req.SmsEnrollmentManagementCollegeMajorBatchReq;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.entity.req.SmsEnrollmentManagementCollegeMajorReq;
import com.xyht.sca_s.student_manage_system.modules.enrollmentManagement.service.SmsEnrollmentManagementCollegeMajorService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.concurrent.Future;

/**
 * <p>
 * 专业院系招生计划表 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/enrollment/collegeMajor")
@SecretBody
public class SmsEnrollmentManagementCollegeMajorController {
    @Resource
    private SmsEnrollmentManagementCollegeMajorService smsEnrollmentManagementCollegeMajorService;
    @Resource
    private JwtTokenUtil jwtTokenUtil;


    @PostMapping("/add")
    @ApiModelProperty(value = "添加院系专业报名计划")
    public ResponseResult addEnrollmentManagementCollegeMajor(@RequestBody SmsEnrollmentManagementCollegeMajorReq collegeMajorReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsEnrollmentManagementCollegeMajorService.addEnrollmentManagementCollegeMajor(collegeMajorReq, user_id);
    }

    @PostMapping("/delete")
    @ApiModelProperty(value = "删除院系专业报名计划")
    public ResponseResult deleteEnrollmentManagementCollegeMajor(@RequestBody SmsEnrollmentManagementCollegeMajorReq collegeMajorReq) {
        return smsEnrollmentManagementCollegeMajorService.deleteEnrollmentManagementCollegeMajor(collegeMajorReq);
    }

    @PostMapping("/update")
    @ApiModelProperty(value = "修改院系专业报名计划")
    public ResponseResult updateEnrollmentManagementCollegeMajor(@RequestBody SmsEnrollmentManagementCollegeMajorReq collegeMajorReq) {
        return smsEnrollmentManagementCollegeMajorService.updateEnrollmentManagementCollegeMajor(collegeMajorReq);
    }

    @GetMapping("/list")
    @ApiModelProperty(value = "获取院系专业报名计划列表")
    public ResponseResult getEnrollmentManagementCollegeMajorList(@RequestParam(value = "taskId") String taskId,
                                                                  @RequestParam(value = "schoolYear", required = false) String schoolYear,
                                                                  @RequestParam(value = "collegeId", required = false) String collegeId,
                                                                  @RequestParam(value = "majorId", required = false) String majorId,
                                                                  @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                  @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
    ) {
        return smsEnrollmentManagementCollegeMajorService.getEnrollmentManagementCollegeMajorList(taskId, collegeId, majorId, schoolYear, pageNum, pageSize);
    }

    @PostMapping("/batch")
    @ApiModelProperty(value = "院系专业报名计划批量导入")
    public ResponseResult batchEnrollmentManagementCollegeMajor(@RequestBody SmsEnrollmentManagementCollegeMajorBatchReq collegeMajorBatchReq, HttpServletRequest request){
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        ResponseResult commonCodeEnum;
        try{
            Future<ResponseResult> future = smsEnrollmentManagementCollegeMajorService.batchEnrollmentManagementCollegeMajor(collegeMajorBatchReq,user_id);
            while (true){
                if (future.isDone()){
                    commonCodeEnum = future.get();
                    break;
                }
            }
        }catch (Exception e){
            e.printStackTrace();
            return CommonResult.failed(CommonCodeEnum.FAIL);
        }
        return commonCodeEnum;
    }
}

